package me.principality.dapp.controller

import me.principality.dapp.domain.model.Menu
import me.principality.dapp.domain.persist.MenuRepository
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*


@RestController
@RequestMapping("/api/menu")
class MenuController(val repository: MenuRepository) {
    @RequestMapping("list", method = arrayOf(RequestMethod.GET))
    fun findAll(): List<Menu> {
        return repository.findAll().list<Menu>();
    }

    @RequestMapping("query", method = arrayOf(RequestMethod.PUT))
    fun query(@RequestBody id: Long): Menu {
        return repository.findById(id)
    }

    @GetMapping("/{id}")
    fun getMenuById(@PathVariable("id") id: Long): ResponseEntity<Menu?>? {
        val menu: Optional<Menu>? = repository.findByIdOptional(id)
        return if (menu != null) {
            ResponseEntity<Menu?>(menu.get(), HttpStatus.OK)
        } else {
            ResponseEntity<Menu?>(HttpStatus.NOT_FOUND)
        }
    }

    @PostMapping("/create")
    fun createMenu(@RequestBody menu: Menu): ResponseEntity<Menu?>? {
        return try {
            repository.persist(menu)
            ResponseEntity<Menu?>(menu, HttpStatus.CREATED)
        } catch (e: Exception) {
            ResponseEntity<Menu?>(null, HttpStatus.INTERNAL_SERVER_ERROR)
        }
    }

    @PutMapping("/update/{id}")
    fun updateMenu(@PathVariable("id") id: Long, @RequestBody menu: Menu): ResponseEntity<Menu?>? {
        val menuTobeUpdated: Menu = repository.findById(id)
        return if (menuTobeUpdated != null) {
//            menuTobeUpdated = menu
            repository.persist(menuTobeUpdated)
            ResponseEntity<Menu?>(menu, HttpStatus.OK)
        } else {
            ResponseEntity<Menu?>(HttpStatus.NOT_FOUND)
        }
    }

    @DeleteMapping("/delete/{id}")
    fun deleteMenu(@PathVariable("id") id: Long): ResponseEntity<HttpStatus?>? {
        return try {
            repository.deleteById(id)
            ResponseEntity<HttpStatus?>(HttpStatus.NO_CONTENT)
        } catch (e: Exception) {
            ResponseEntity<HttpStatus?>(HttpStatus.INTERNAL_SERVER_ERROR)
        }
    }

    @DeleteMapping("/deleteAll")
    fun deleteAllMenus(): ResponseEntity<HttpStatus?>? {
        return try {
            repository.deleteAll()
            ResponseEntity<HttpStatus?>(HttpStatus.NO_CONTENT)
        } catch (e: Exception) {
            ResponseEntity<HttpStatus?>(HttpStatus.INTERNAL_SERVER_ERROR)
        }
    }

//    @GetMapping("/menu/published")
//    fun findByPublished(): ResponseEntity<List<Menu>?>? {
//        return try {
//            val Menus: List<Menu> = MenuRepository.findByPublished(true)
//            if (Menus.isEmpty()) {
//                ResponseEntity<List<Menu>?>(HttpStatus.NO_CONTENT)
//            } else ResponseEntity<List<Menu>?>(Menus, HttpStatus.OK)
//        } catch (e: Exception) {
//            ResponseEntity<List<Menu>?>(HttpStatus.INTERNAL_SERVER_ERROR)
//        }
//    }
}